var pageSize=10;
var params = {
    start: 1,
    size: pageSize,
    addressCode: '',
    branchName: '',
    statisticsTime: '',
    endTime: ''
};
var selectObj=[];
var selectAllId=[];

$(window).ready(function () {
    //时间选择器
    timeUtil.select('#statistics-time');
      //时间选择器
        timeUtil.select('#end-time');
    //加载物流信息
    loadLogistics();
    //加载区域信息
    if (orgClass == "org_ebb_city") {
        fillCounty("520600000000", "addressCode");
        $("#addressCode").change(function () {
            params.addressCode = this.value;
            loadLogistics();
        });
    } else {
        $("#address-box").hide();//隐藏区域筛选下拉列表
    }

    $(".file").on("change", "input[type='file']", function () {
        var filePath = $(this).val();
        if (filePath.indexOf("xls") != -1 || filePath.indexOf("xlsx") != -1) {
            $(".fileerrorTip").html("").hide();
            var arr = filePath.split('\\');
            var fileName = arr[arr.length - 1];

            $(".showFileName").html(fileName);

            $("#importbtn").removeAttr("display");
            $("#importbtn").attr("style", "display:inline");
        } else {
            $(".showFileName").html("");
            $(".fileerrorTip").html("您未上传文件，或者您上传文件类型有误！").show();
            return false
        }
    });
    var options = {
        url: '/ebb/logistics/import',
        type: 'post',
        success: function (data) {
            console.log(data);
            if (!data.error) {
                $("#importing").attr("style", "display:none");
                alert("导入成功!");
                location.reload();
            } else {
                if (data.errorMessage) {
                    alert(data.errorMessage);
                }
            }
        },
        error: function () {
            $("#importing").attr("style", "display:none");
            alert("导入失败!");
            location.reload();
        }
    };
    var lock = false;
    $("form[name=imporexcelform]").submit(function () {
        if (lock) {
            alert("不能重复提交");
            return;
        }
        lock = true;
        $(".btn-info").attr('disabled', true);
        $(this).ajaxSubmit(options);
        return false; //阻止表单默认提交
    });
    //检索
    $('#submit').click(function () {
        var branchName = $('#branchName').val().trim();
        params.branchName = branchName;
        params.statisticsTime = $('#statistics-time').val().trim();
        params.endTime = $('#end-time').val().trim();
        loadLogistics();
    });
    //分页跳转
    $('#go-index').click(function () {
        var pageIndex = $('#page-index').val();
        params.start = parseInt(pageIndex);
        loadLogistics();
    });
    //导出
    $('#export').click(function () {
        exportLogistics();
    });
});
//加载物流信息
function loadLogistics() {
    $.ajax({
        url: '/ebb/logistics/latest',
        type: 'get',
        data: params,
        success: function (result) {
            if (result.data.length > 0) {
                $("#total-page").text(result.totalPages);
                $("#pagination-bar").show();
                $("#pagination-box").html('<ul id="pagination" class="pagination"></ul>');
                $('#pagination').twbsPagination({
                    totalPages: result.totalPages,
                    visiblePages: 5,
                    first: '第一页',
                    prev: '上一页',
                    next: '下一页',
                    last: '最后页',
                    onPageClick: function (event, page) {
                        params.start = page;
                        $.ajax({
                            url: '/ebb/logistics/latest',
                            type: 'get',
                            data: params,
                            success: function (result) {
                                console.log(result);
                                selectObj = result.data;
                                $("input[name='checkboxAll']").prop("checked",false);
                                selectAllId=[];
                                $.templates("#logistics-list-tmpl").link("#logistics-list-data", result.data);
                                $('#page-index').val(page);
                                $('#total-page').text(result.totalPages);
                                $('#total-count').text(result.totalCount);
                                $.each(result.data, function (i, item) {
                                    //删除物流信息
                                    $('#logistics-delete-' + i).click(function () {
                                        if (!confirm('您确定要删除吗？')) {
                                            return;
                                        }
                                        deleteLogisticsBusiness(item.logisticsBusinessId);
                                    });
                                    //修改物流信息
                                    $('#logistics-edit-' + i).click(function () {
                                        window.location.href = 'editLogistics.html?id=' + item.logisticsId;
                                    });
                                    //添加物流业务量
                                    $('#logistics-add-' + i).click(function () {
                                        window.location.href = 'editLogisticsBusiness.html?logisticsId=' + item.logisticsId;
                                    });
                                });
                            }
                        });
                    },
                    startPage: params.start
                });
            } else {
                $("#logistics-list-data").html("<tr><td colspan='13'><h1 align='center' height='300px'>暂无数据</h1></td></tr>");
                $("#pagination-bar").hide();
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            if (XMLHttpRequest.status == 401) {
                //跳转到登录页,带上登录后的跳转地址redirect_uri=???
                // redirect_login();
                ShowTip("你还没有登录，请先登录！", "success", 1000);
                setTimeout(function () {
                    window.location = "login.html";
                }, 1000);
            }
        }
    });
}

function selectall(obj) {
    selectAllId=[];
    //获取checked属性
    if ($(obj).prop("checked")) {
        //设置checked属性
        $("input[name='checkboxAll']").prop("checked", true);
        $("input[name='check']").prop("checked", true);
        for(var i=0;i<selectObj.length;i++){
            selectAllId.push(selectObj[i].logisticsId);
        }
    } else {
        //设置checked属性
        $("input[name='checkboxAll']").prop("checked",false);
        $("input[name='check']").prop("checked", false);
    }
}

function selectsub(id) {
    //获取checked属性
    if ($("#"+id).prop("checked")) {
        //设置checked属性
        $("input[id=id]").prop("checked", true);
        var count=0;
        for(var i=0;i<selectAllId.length;i++){
            if(selectAllId[i]==id){
                count+=1;
            }
        }
        if(count==0){
            selectAllId.push(id);
        }
    } else {
        //设置checked属性
        $("input[id=id]").prop("checked",false);
        for(var i=0;i<selectAllId.length;i++){
            if(selectAllId[i]==id){
                selectAllId.splice(i,1);
            }
        }
    }
    if(selectAllId.length==selectObj.length){
        $("input[name='checkboxAll']").prop("checked", true);
    }else{
        $("input[name='checkboxAll']").prop("checked",false);
    }
}

function deleteSelectedAll() {
    if(selectAllId.length==0){
        ShowTip("请选择需要删除的项！", "danger", 1000);
        return;
    }
    var ids='';
    for(var i=0;i<selectAllId.length;i++){
        ids+="'"+selectAllId[i]+"'"+","
    }
    ids=ids.substring(0,ids.length-1);

    Showbox.Msg.confirm('如果有相关的学员信息，会一起删除，您确定删除所选记录吗？',function(v){
        if(v=='yes'){
            $.ajax({
                type: "delete",
                url: "/ebb/logistics/deleteSelectedAll/",
                contentType: 'application/json',
                data: ids,
                success: function (data, status) {
                    if(data>0){
                        var msg="";
                        if(data>pageSize){
                            msg=",其中物流数据"+pageSize+"条，对应业务数据"+(data-pageSize)+"条";
                        }
                        ShowTip("删除成功"+data+"条"+msg+"！", "success", 2000);
                        loadLogistics();
                    }else{
                        ShowTip("删除失败！", "danger", 1000);
                    }
                },
                error:function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest);
                    ShowTip(XMLHttpRequest, "error", 1000);
                }
            });
        }
        if(v=='no'){
        }
    });
}

//删除物流信息
function deleteLogistics(id) {
    $.ajax({
        url: '/ebb/logistics/?id=' + id,
        type: 'delete',
        success: function (result) {
            console.log(result);
            loadLogistics();
        }
    });
}
//删除物流业务量信息
function deleteLogisticsBusiness(id) {
    $.ajax({
        url: '/ebb/logistics/business/' + id,
        type: 'delete',
        success: function (result) {
            console.log(result);
            loadLogistics();
        }
    });
}
//导出物流信息
function exportLogistics() {
    window.location.href = "/ebb/logistics/export?"
        + "&addressCode=" + params.addressCode
        + "&branchName=" + params.branchName
               + "&statisticsTime=" + params.statisticsTime
                + "&endTime=" + params.endTime;;
        // /logistics/export
}

